﻿/*******************************************************************************
Copyright (c) 2012-2013, PKStudio

http://pkstudio.org

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*******************************************************************************/

using System;
using System.Diagnostics;
using System.Drawing;
using System.IO;

namespace PKEngine
{
	/// <summary>
	/// PKLight can be drawn on screen with alpha channel and color.
	/// </summary>
	public class PKLight : PKNode
	{
		#region Private Fields
		private Rectangle rect;
		#endregion

		#region Public Properties
		/// <summary>
		/// Channel of PKLight.
		/// </summary>
		public PKChannel Channel { get; private set; }

		/// <summary>
		/// Rect of PKLight.
		/// </summary>
		public Rectangle Rect
		{
			get
			{
				return this.rect;
			}
			private set
			{
				if (!this.rect.Equals(value))
				{
					this.rect = value;
					this.ContentSize = new Size(value.Width, value.Height);
				}
			}
		}

		/// <summary>
		/// Color of PKLight.
		/// </summary>
		public ushort Color { get; set; }
		#endregion

		#region Initialization
		protected PKLight()
			: base()
		{
			this.Color = PKAlpha.SharedAlpha.ColorWhite;
		}

		public PKLight(PKChannel channel)
			: this()
		{
			Debug.Assert(channel != null, "PKLight: Argument cannot be null.");

			this.Channel = channel;
			this.Rect = new Rectangle(0, 0, channel.ContentSize.Width, channel.ContentSize.Height);
		}

		public PKLight(PKChannel channel, Rectangle rect)
			: this()
		{
			Debug.Assert(channel != null, "PKLight: Argument cannot be null.");

			this.Channel = channel;
			this.Rect = rect;
		}

		public PKLight(string fileName)
			: this()
		{
			Debug.Assert(!string.IsNullOrEmpty(fileName), "PKLight: Argument cannot be null.");
			Debug.Assert(File.Exists(fileName), "PKLight: Cannot find file.");

			PKChannel channel = new PKChannel(fileName);
			if (channel != null)
			{
				this.Channel = channel;
				this.Rect = new Rectangle(0, 0, channel.ContentSize.Width, channel.ContentSize.Height);
			}
		}

		public PKLight(string fileName, Rectangle rect)
			: this()
		{
			Debug.Assert(!string.IsNullOrEmpty(fileName), "PKLight: Argument cannot be null.");
			Debug.Assert(File.Exists(fileName), "PKLight: Cannot find file.");

			PKChannel channel = new PKChannel(fileName);
			if (channel != null)
			{
				this.Channel = channel;
				this.Rect = rect;
			}
		}
		#endregion

		#region Public Methods
		/// <summary>
		/// Draws light.
		/// </summary>
		public override void Draw()
		{
			PKMainSurface.SharedMainSurface.FillWithAlphaChannel(this.BoundingBox.X, this.BoundingBox.Y, this.Channel, this.Rect, this.Color);
		}
		#endregion
	}
}
